﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace LotteryTicket
{
    class FileHandle
    {
       private FileStream fs_output1;
       private StreamWriter sw_output1;
       private FileStream fs_output2;
       private StreamWriter sw_output2;

       public string sourceFile = "Resource/output.txt";
       public string outputFile1 = "Cache/History/";
       public string outputFile2 = "Cache/Origin/";

        private void createstream1(string FileName)
        {
            fs_output1 = new FileStream(outputFile1+FileName, FileMode.Create);
            sw_output1 = new StreamWriter(fs_output1);
        }
        private void createstream2(string FileName)
        {
            fs_output2 = new FileStream(outputFile2 + FileName, FileMode.Create);
            sw_output2 = new StreamWriter(fs_output2);
        }
        private void closestream()
        {
            if (sw_output1 != null)
                sw_output1.Close();
            if (fs_output1 != null)
                fs_output1.Close();//这里要注意fs一定要在sw后面关闭，否则会抛异常
            if (sw_output2 != null)
                sw_output2.Close();
            if (fs_output2 != null)
                fs_output2.Close();//这里要注意fs一定要在sw后面关闭，否则会抛异常
        }
        private void writefile1(string line)
        {
            sw_output1.WriteLine(line);
        }

        private void writefile2(string line)
        {
            sw_output2.WriteLine(line);
        }

        /// <summary>
        /// 获取id最大值
        /// </summary>
        /// <returns></returns>
        public int GetMaxIndex()
        {
            StreamReader sr = new StreamReader("Cache/Origin/base.txt", Encoding.Default);
            String line;
            int flag = 0;
            int index = 0;

            while ((line = sr.ReadLine()) != null)
            {
                if (line == "#")
               {
                   flag ++;
                   if(flag == 2)
                   {
                       flag = 0;
                       
                   }
               }
               else if (flag == 1)
                {
                    if (line == "total")
                    {
                        flag = 3;
                    }
      
                }else if(flag == 3){
                   // MessageBox.Show(line);
                    flag = 1;
                    if(line != "")
                    index = int.Parse(line);
                }

               // break;
            }

            sr.Close();
        
            return index;
        }

        public string GetLastInfo(int appear)
        {
            StreamReader sr = new StreamReader(outputFile1+"appear"+appear, Encoding.Default);
            String line;
            int flag = 0;
            int index = 0;
            string info = "";
            index = GetMaxIndex();
            while ((line = sr.ReadLine()) != null)
            {
                
                string[] sArray = line.Split(' ');
                if (sArray[0] == index.ToString())
                {
                    info = line;
                    break;
                }
            }

            sr.Close();

            return info;
        }
        /// <summary>
        /// 计算Appear300 数据
        /// </summary>
        public void CalculateAppear(int appear)
        {
            StreamReader sr = new StreamReader(outputFile2+"base.txt", Encoding.Default);
            String line;
            int flag = 0;
            int start = 0;
            int total = 0;

           // max = GetMaxIndex();

            start =  1;//从第1行开始
            int[] appear300A = new int[1000];

            createstream1("appear"+appear);
            while ((line = sr.ReadLine()) != null)
            {
                string[] sArray = line.Split(' ');
                if (sArray[0] == start.ToString())
                {
                    string t = sArray[3] + sArray[4] + sArray[5];
                    line = sArray[0] + " " + sArray[1] + " " + sArray[2] + " " + sArray[3] + " " + sArray[4] + " " + sArray[5] + " " + sArray[6] + " " + sArray[7];
                    if (start > appear)
                    {
                        if ((start - appear300A[int.Parse(t)]) <= appear)
                        {
                            flag++;
                            line += " " + flag;
                            total++;
                        }
                        else
                        {
                            flag = 0;
                            line += " 0";
                        }
                    }
                    else
                    {
                        line += " 0";
                    }
                //    if (0 == appear300A[int.Parse(t)])
                        appear300A[int.Parse(t)] = start;
                    //else if (0 == appear300B[int.Parse(t)])
                    //    appear300B[int.Parse(t)] = start;
                    //else {
                    //    appear300C[int.Parse(t)] = start;
                    //} 

                    start++;
                }
                writefile1(line);
                if (line == "end")
                {
                    line += total;
                }
            }
            sr.Close();
            start -= 500;
            MessageBox.Show(""+total+"/"+start);
            closestream();
        }

        /// <summary>
        /// 计算跨度相同的
        /// </summary>
        public void CalculateSpan()
        {
            StreamReader sr = new StreamReader(outputFile2 + "base.txt", Encoding.Default);
            String line;
            String pre ="";
            int start = 0;
            int flag = 0;

            // max = GetMaxIndex();

            start = 1;//从第1行开始
 
            createstream1("span");
            while ((line = sr.ReadLine()) != null)
            {
                string[] sArray = line.Split(' ');
                if (sArray[0] == start.ToString())
                {
                    string t = sArray[3] + sArray[4] + sArray[5]; 
                    line = sArray[0] + " " + sArray[1] + " " + sArray[2] + " " + sArray[3] + " " + sArray[4] + " " + sArray[5]+ " " + sArray[6]+" "+sArray[7];
                    if(pre == sArray[6])
                        {
                            flag++;
                            line += " " + flag;
                        }
                        else
                        {
                            flag = 0;
                            line += " 0";
                        }
                    pre = sArray[6];
                    start++;
                }
                writefile1(line);
            
            }
            sr.Close();
            closestream();
        }

        /// <summary>
        /// 计算概率
        /// </summary>
        public void CalculateSpan_p(string span_now)
        {
            StreamReader sr = new StreamReader(outputFile1 + "span", Encoding.Default);
            String line;
            int pre =0;
            int start = 0;
            int flag = 0;

            // max = GetMaxIndex();

            start = 1;//从第1行开始
            string last = "";
            while ((line = sr.ReadLine()) != null)
            {
                string[] sArray = line.Split(' ');
                if (sArray[0] == start.ToString())
                {
                    string t = sArray[3] + sArray[4] + sArray[5];
                    line = sArray[0] + " " + sArray[1] + " " + sArray[2] + " " + sArray[3] + " " + sArray[4] + " " + sArray[5] + " " + sArray[6];
                    if (span_now == last)
                    {
                        if ("0" == sArray[7])
                        { flag++; }
                        else 
                            pre++;
                        }
                    last = sArray[7];
                    start++;
                }
            }
            sr.Close();

            MessageBox.Show(""+flag+"-"+pre);
        }


        public void CalculateThree_p()
        {
            StreamReader sr = new StreamReader(outputFile1 + "span", Encoding.Default);
            String line;
            int pre = 0;
            int start = 0;
            int flag = 0;

            // max = GetMaxIndex();

            start = 1;//从第1行开始
            string last_span = "";
            string last_sum = "";

            int count_appear = 0;
            int count_disppear = 0;
            while ((line = sr.ReadLine()) != null)
            {
                string[] sArray = line.Split(' ');
                if (sArray[0] == start.ToString())
                {
                    string t = sArray[3] + sArray[4] + sArray[5];
                   // line = sArray[0] + " " + sArray[1] + " " + sArray[2] + " " + sArray[3] + " " + sArray[4] + " " + sArray[5] + " " + sArray[6];
                    if (start >500)
                    {
                        if ((last_span != sArray[6]) && ("0" == sArray[8]) && (last_sum != sArray[7]))
                            count_appear++;
                        else
                            count_disppear++;
                    }
                    last_span = sArray[6];
                    last_sum = sArray[7];

                    start++;
                }
            }
            sr.Close();

            MessageBox.Show("" + count_appear + "-" + count_disppear);
        }

        /// <summary>
        /// CalculateAppear_p(500，1)；//表示 计算circle500 ，中本期出的数(1)是前500期出过，下期出现 为0的概率( 统计概率)
        /// </summary>
        /// <param name="appear">appear300/500/1000 </param>
        /// <param name="now_appear">计算当前情况下，下期出现数不在500期内出现的概率</param>
        public int CalculateAppear_p(int appear,int now_appear)
        {
            StreamReader sr = new StreamReader(outputFile1 + "appear"+appear, Encoding.Default);
            String line;
            int total = 0;
            int start = 0;
            int pre = 0;
            int tmp = 0;
            // max = GetMaxIndex();

            start = 1;//从第1行开始
            //int[] p = new int[5];//概率次数,只计算 下一期 是前500期没出现的概率，即 0->0 ,1->0 ,2->0 ...
            int p = 0;
      //      createstream1("appear_p");
            while ((line = sr.ReadLine()) != null)
            {
                string[] sArray = line.Split(' ');
                if (sArray[0] == start.ToString())
                {
                    if (start > appear)
                    {
                        if (pre == now_appear) {
                            if (sArray[6] == "0")
                            {
                                p++;
                            }
                            else { tmp++; }
                            total++;
                        }
                    }
                    pre = int.Parse(sArray[6]);
                    start++;
                }
        //        writefile1(line);
               
            }
            sr.Close();
     //       closestream();
            /*下期 不在前500期内 概率*/
            return (p * 10 / total);
            /*下期 在前500期内 概率*/
           // return (tmp*10/total);
        }
       

        /// <summary>
        /// 获取 近appear 出现或未出现的数 
        /// </summary>
        /// <param name="appear">300/500/1000</param>
        /// <param name="flag">"appear",出现 "disppear"未出现</param>
        public void GetAppear(int appear,string flag)
        {
            StreamReader sr = new StreamReader(outputFile2+"base.txt", Encoding.Default);
            String line;
            int start = 0;
            int max = 0;

            max = GetMaxIndex();

            start = max - appear;//选取最近appear期
            string[] appear_code = new string[1000];


            while ((line = sr.ReadLine()) != null)
            {
                string[] sArray = line.Split(' ');
                if (sArray[0] == start.ToString())
                {
                    start++;
                    string t = sArray[3]+sArray[4]+sArray[5];
                    appear_code[int.Parse(t)] = t;
                }
                if (start > max)
                    break;
            }
            sr.Close();
            /*appear 300*/
            createstream1("appear_data"+appear);
            writefile1("# appear"+appear);
            int j = 0;
            if (flag == "appear")
            {
                for (int i = 0; i < 1000; i++)
                    if (appear_code[i] != null)
                    {
                        writefile1(appear_code[i]);
                        j++;
                    }
                writefile1("# end appear " + appear + "-" + j);
            }
            else
            {
                /*disappear300*/
                writefile1("# disappear"+appear);
                j = 0;
                for (int i = 0; i < 1000; i++)
                    if (appear_code[i] == null)
                    {
                        writefile1(string.Format("{0:D3}", i));
                        j++;
                    }
                writefile1("# end disappear"+appear+" -" + j);
            }
            closestream();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="h"></param>
        /// <param name="t"></param>
        /// <param name="s"></param>
        /// <returns>0：通过，1：不过</returns>
        public int signal_filter(string source,int span,int sum)
        {
            int max;
            int min;
            int num;
            int h=0;
            int t=0;
            int s=0;

            num = int.Parse(source);

            h = num / 100;
            t = num % 100 / 10;
            s = num % 10;
          //  MessageBox.Show(""+h+"-"+t+"-"+s);
            max =h;
            min =h;
            if (min > t)
                min = h;

            if (min > s)
                min = s;

            if (max < t)
                max = t;

            if (max < s)
                max = s;


            int sum_t = h + t + s;
            int span_t = max -min;
            if ((sum_t != sum) && (span != span_t))
                return 0;
            else
                return 1;
        }

        public void GetAppearFilter(int appear)
        {
            StreamReader sr = new StreamReader(outputFile1 + "appear_data"+appear, Encoding.Default);
            String line;
            int start = 0;
            int max = 0;


            string[] appear_code = new string[1000];
            string info = GetLastInfo(appear); 
            string[] s_a = info.Split(' ');
            int span = int.Parse(s_a[6]);
            int sum = int.Parse(s_a[7]);
            createstream1("end_data" + appear);
            while ((line = sr.ReadLine()) != null)
            {
                string[] sArray = line.Split(' ');
                if (sArray[0] != "#")
                {
                  
                   int ret = signal_filter(line,span,sum);
                   if (0 == ret)
                   {
                       writefile1(line);
                       start++;
                   }
                   else max++;

                }
                else
                {
                    writefile1(line);
                }
             
            }
            sr.Close();
            MessageBox.Show("y"+start +" no :"+max);
            closestream();
        }
        /// <summary>
        /// 计算前10期出现某数字出现最高次数
        /// </summary>
        /// <param name="appear"></param>
        public void CalculateCircle10_max(int appear)
        {
            StreamReader sr = new StreamReader(outputFile2 + "base.txt", Encoding.Default);
            String line;
            int flag = 0;
            int start = 0;
            int max = 0;
            int index = 0;
            int crc = 0;
            int max_count = 0;
            // max = GetMaxIndex();

            start = 1;//从第1行开始
            int[] circle_h = new int[10];
            int[] circle_t = new int[10];
            int[] circle_s = new int[10];
            int[] circle = new int[10];
            createstream1("output_circle10");
            while ((line = sr.ReadLine()) != null)
            {
                string[] sArray = line.Split(' ');
                max_count = 0;

                if (sArray[0] == start.ToString())
                {
                    line = sArray[0] + " " + sArray[1] + " " + sArray[2] + " " + sArray[3] + " " + sArray[4] + " " + sArray[5];
                    if (index == 10)
                        index = 0;
                    if (start > 10)
                    {
                        circle[circle_h[index]]--;
                        circle[circle_t[index]]--;
                        circle[circle_s[index]]--;
                    }
                    circle_h[index] = int.Parse(sArray[3]);
                    circle_t[index] = int.Parse(sArray[4]);
                    circle_s[index] = int.Parse(sArray[5]);
                    circle[circle_h[index]]++;
                    circle[circle_t[index]]++;
                    circle[circle_s[index]]++;
                    if (start >= 10)
                    {
                        max = circle[0];
                        crc = circle[0];
                        for (int i = 1; i < 10; i++)
                        {
                            crc += circle[i];
                            if (max < circle[i])
                                max = circle[i];
                        }
                        for (int i = 0; i < 10; i++)
                        {
                            if (max == circle[i])
                                max_count++;
                        }
                        
                        switch (max)
                        { 
                            case 10:
                                line += " A";
                                break;
                            case 11:
                                line += " B";
                                break;
                            case 12:
                                line += " C";
                                break;
                            case 13:
                                line += " D";
                                break;
                            case 14:
                                line += " E";
                                break;
                            case 15:
                                line += " F";
                                break;
                            default:
                                line += " " + max;
                                break;
                        }
                        line += " " + max_count;
                        if (crc != 30)
                            MessageBox.Show("crc error"+crc);
                        
                    }
                    else
                    {
                        line += " 0 0";
                    }
                    //    if (0 == appear300A[int.Parse(t)])
                   // appear300A[int.Parse(t)] = start;
                    //else if (0 == appear300B[int.Parse(t)])
                    //    appear300B[int.Parse(t)] = start;
                    //else {
                    //    appear300C[int.Parse(t)] = start;
                    //} 

                    start++;
                    index++;
                    
                }
                writefile1(line);

            }
            sr.Close();

            closestream();
        }

        public void CalculateCircle5_max(int appear)
        {
            StreamReader sr = new StreamReader(outputFile2 + "base.txt", Encoding.Default);
            String line;
            int flag = 0;
            int start = 0;
            int max = 0;
            int index = 0;
            int crc = 0;
            int max_count = 0;
            // max = GetMaxIndex();

            start = 1;//从第1行开始
            int[] circle_h = new int[10];
            int[] circle_t = new int[10];
            int[] circle_s = new int[10];
            int[] circle = new int[10];
            createstream1("output_circle5");
            while ((line = sr.ReadLine()) != null)
            {
                string[] sArray = line.Split(' ');
                max_count = 0;

                if (sArray[0] == start.ToString())
                {
                    line = sArray[0] + " " + sArray[1] + " " + sArray[2] + " " + sArray[3] + " " + sArray[4] + " " + sArray[5];
                    if (index == 5)
                        index = 0;
                    if (start > 5)
                    {
                        circle[circle_h[index]]--;
                        circle[circle_t[index]]--;
                        circle[circle_s[index]]--;
                    }
                    circle_h[index] = int.Parse(sArray[3]);
                    circle_t[index] = int.Parse(sArray[4]);
                    circle_s[index] = int.Parse(sArray[5]);
                    circle[circle_h[index]]++;
                    circle[circle_t[index]]++;
                    circle[circle_s[index]]++;
                    if (start >= 5)
                    {
                        max = circle[0];
                        crc = circle[0];
                        for (int i = 1; i < 10; i++)
                        {
                            crc += circle[i];
                            if (max < circle[i])
                                max = circle[i];
                        }
                        for (int i = 0; i < 10; i++)
                        {
                            if (max == circle[i])
                                max_count++;
                        }

                        switch (max)
                        {
                            case 10:
                                line += " A";
                                break;
                            case 11:
                                line += " B";
                                break;
                            case 12:
                                line += " C";
                                break;
                            case 13:
                                line += " D";
                                break;
                            case 14:
                                line += " E";
                                break;
                            case 15:
                                line += " F";
                                break;
                            default:
                                line += " " + max;
                                break;
                        }
                        line += " " + max_count;
                        if (crc != 15)
                            MessageBox.Show("crc error" + crc);

                    }
                    else
                    {
                        line += " 0 0";
                    }
                    //    if (0 == appear300A[int.Parse(t)])
                    // appear300A[int.Parse(t)] = start;
                    //else if (0 == appear300B[int.Parse(t)])
                    //    appear300B[int.Parse(t)] = start;
                    //else {
                    //    appear300C[int.Parse(t)] = start;
                    //} 

                    start++;
                    index++;

                }
                writefile1(line);

            }
            sr.Close();

            closestream();
        }

        /// <summary>
        /// 计算前10期出现某数字出现最高次数
        /// </summary>
        /// <param name="appear"></param>
        public void CalculateCircle10_min(int appear)
        {
            StreamReader sr = new StreamReader(outputFile2 + "output_circle10", Encoding.Default);
            String line;
            int flag = 0;
            int start = 0;
            int max = 0;
            int index = 0;
            int crc = 0;

            // max = GetMaxIndex();

            start = 1;//从第1行开始
            int[] circle_h = new int[10];
            int[] circle_t = new int[10];
            int[] circle_s = new int[10];
            int[] circle = new int[10];
            createstream2("output_circle10_min");
            while ((line = sr.ReadLine()) != null)
            {
                string[] sArray = line.Split(' ');
                if (sArray[0] == start.ToString())
                {
                    if (index == 10)
                        index = 0;
                    if (start > 10)
                    {
                        circle[circle_h[index]]--;
                        circle[circle_t[index]]--;
                        circle[circle_s[index]]--;
                    }
                    circle_h[index] = int.Parse(sArray[3]);
                    circle_t[index] = int.Parse(sArray[4]);
                    circle_s[index] = int.Parse(sArray[5]);
                    circle[circle_h[index]]++;
                    circle[circle_t[index]]++;
                    circle[circle_s[index]]++;
                    
                    if (start >= 10)
                    {
                        max = circle[0];
                        crc = circle[0];
                        for (int i = 1; i < 10; i++)
                        {
                            crc += circle[i];
                            if (max > circle[i])
                                max = circle[i];
                        }
                        line += " " + max;
                        if (crc != 30)
                            MessageBox.Show("error"+crc);

                    }
                    else
                    {
                        line += " 0";
                    }
                    //    if (0 == appear300A[int.Parse(t)])
                    // appear300A[int.Parse(t)] = start;
                    //else if (0 == appear300B[int.Parse(t)])
                    //    appear300B[int.Parse(t)] = start;
                    //else {
                    //    appear300C[int.Parse(t)] = start;
                    //} 

                    start++;
                    index++;

                }
                writefile2(line);

            }
            sr.Close();

            closestream();
        }
        /// <summary>
        /// 计算前10期出现某数字出现最高次数
        /// </summary>
        /// <param name="appear"></param>
        public void CalculateCircle10_p(int appear)
        {
            StreamReader sr = new StreamReader(outputFile2 + "output_circle10", Encoding.Default);
            String line;
            int flag = 0;
            int start = 0;
            int max = 0;
            int last = 0;
            int total=0;
            // max = GetMaxIndex();

            start = 1;//从第1行开始

            int[] circle = new int[10];
            flag =0;
            while ((line = sr.ReadLine()) != null)
            {
                string[] sArray = line.Split(' ');
                if (sArray[0] == start.ToString())
                { 
                    
                    if (start > 10)
                    {
                        total++;
                        if (sArray[11] == "A") sArray[11] = "10";
                        else if (sArray[11] == "B") sArray[11] = "11";
                        else if (sArray[11] == "C") sArray[11] = "12";
                        else if (sArray[11] == "D") sArray[11] = "13";
                        else if (sArray[11] == "E") sArray[11] = "14";
                        else if (sArray[11] == "F") sArray[11] = "15";


                        last = int.Parse(sArray[11]);
                        if ((last == max))// || ((last + 1) == max) || (last - 1) == max)
                        { flag++; }
                    }
                    else max = int.Parse(sArray[11]);
                    start++;
                }
            }
            sr.Close();
            MessageBox.Show("t: "+total+"- y:"+flag);
        }
    }
}
